﻿Imports Odyssee.WebControles
Imports OpenDelib.ServiceDeliberations

Public Class GestionVotes
    Inherits OpenDelibPage

    Public Enum EtapeVote
        ChoixAssemblee = 0
        ChoixRapport = 1
        AffectationVotes = 2
        VisualisationVotes = 3
    End Enum

    Private ReadOnly Property EtatAssembleeEnCours As String
        Get
            Return Tools.GetInstance().Parametre(TypeParametre.EtatDossierEnCours)
        End Get
    End Property
    Protected ReadOnly Property TypeRelationParticipant As String
        Get
            Return Tools.GetInstance().Parametre(TypeParametre.TypeRelationParticipant)
        End Get
    End Property
    Protected ReadOnly Property TypeRelationVote As String
        Get
            Return Tools.GetInstance().Parametre(TypeParametre.TypeRelationVote)
        End Get
    End Property
    Protected ReadOnly Property CodeZSTypeRelationVoteOui As String
        Get
            Return Tools.GetInstance().Parametre(TypeParametre.CodeZSTypeRelationVoteOui)
        End Get
    End Property
    Protected ReadOnly Property CodeZSTypeRelationVoteNon As String
        Get
            Return Tools.GetInstance().Parametre(TypeParametre.CodeZSTypeRelationVoteNon)
        End Get
    End Property
    Protected ReadOnly Property CodeZSTypeRelationVoteAbstention As String
        Get
            Return Tools.GetInstance().Parametre(TypeParametre.CodeZSTypeRelationVoteAbstention)
        End Get
    End Property
    Protected ReadOnly Property CodeZSTypeRelationVoteAbsent As String
        Get
            Return Tools.GetInstance().Parametre(TypeParametre.CodeZSTypeRelationVoteAbsent)
        End Get
    End Property
    Public ReadOnly Property AssembleesSelectionnees As List(Of Assemblee)
        Get
            Dim liste As New List(Of Assemblee)

            For Each row As GridViewRow In gvAssemblees.Rows
                If CType(row.Cells(0).Controls(0), CheckBox).Checked Then liste.Add(Tools.GetRowDataItem(Of Assemblee, List(Of Assemblee))(Me.gvAssemblees, row))
            Next

            Return liste
        End Get
    End Property
    Public ReadOnly Property RapportsSelectionnes As List(Of Rapport)
        Get
            Dim liste As New List(Of Rapport)

            For Each row As GridViewRow In gvRapports.Rows
                If CType(row.Cells(0).Controls(0), CheckBox).Checked Then liste.Add(Tools.GetRowDataItem(Of Rapport, List(Of Rapport))(Me.gvRapports, row))
            Next

            Return liste
        End Get
    End Property
    Private Property Etape As EtapeVote
        Get
            If ViewState("etape") Is Nothing Then ViewState("etape") = EtapeVote.ChoixAssemblee
            Return CType(ViewState("etape"), EtapeVote)
        End Get
        Set(value As EtapeVote)
            ViewState("etape") = value
        End Set
    End Property
    Private Property Assemblee As Assemblee
        Get
            Return CType(ViewState("assemblee"), Assemblee)
        End Get
        Set(value As Assemblee)
            ViewState("assemblee") = value
        End Set
    End Property
    Private Property Rapport As Rapport
        Get
            Return CType(ViewState("rapport"), Rapport)
        End Get
        Set(value As Rapport)
            ViewState("rapport") = value
        End Set
    End Property
    Private ReadOnly Property Votes As List(Of Vote)
        Get
            If ViewState("votes") Is Nothing Then ViewState("votes") = New List(Of Vote)
            Return CType(ViewState("votes"), List(Of Vote))
        End Get
    End Property



    Protected Overrides Sub OnInit(e As EventArgs)
        Dim appPath As String = Request.ApplicationPath
        If appPath = "/" Then
            appPath = ""
        End If

        OpenDelibPage.AddJS(Me.Page, "selectioncontrole", appPath & "/Scripts/selection.js")

        MyBase.OnInit(e)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Master.Titre = "Gestion des votes"
        If Not IsPostBack Then
            InitialiserEtape(True)
        End If
        InitialiserTableauSaisie()
    End Sub

    Private Sub btnPrecedent_Click(sender As Object, e As EventArgs) Handles btnPrecedent.Click
        If CInt(Me.Etape) - 1 < 0 Then Return
        Me.Etape = CType(CInt(Me.Etape) - 1, EtapeVote)
        InitialiserEtape(True)
    End Sub
    Private Sub btnSuivant_Click(sender As Object, e As EventArgs) Handles btnSuivant.Click
        If CInt(Me.Etape) + 1 > 3 Then Return

        Select Case Me.Etape
            Case EtapeVote.ChoixAssemblee
                If Me.AssembleesSelectionnees.Count <> 1 Then
                    NotifyErreur("Vous devez sélectionner une assemblée.")
                    Return
                Else
                    Me.Assemblee = Me.AssembleesSelectionnees(0)
                End If
            Case EtapeVote.ChoixRapport
                If Me.RapportsSelectionnes.Count <> 1 Then
                    NotifyErreur("Vous devez sélectionner un rapport.")
                    Return
                Else
                    Me.Rapport = Me.RapportsSelectionnes(0)

                End If
            Case EtapeVote.AffectationVotes,
                 EtapeVote.VisualisationVotes
                Return
        End Select

        Me.Etape = CType(CInt(Me.Etape) + 1, EtapeVote)

        InitialiserEtape(True)
    End Sub
    Private Sub btnAnnuler_Click(sender As Object, e As EventArgs) Handles btnAnnuler.Click
        ReinitialiserPage()
    End Sub
    Private Sub btnEnregistrer_Click(sender As Object, e As EventArgs) Handles btnEnregistrer.Click
        If Me.tbVotes.Rows.Count <= 1 Then
            NotifyErreur("Aucune personne ne peut effectuer un vote !")
            Return
        End If

        Dim erreurs As String = ""
        Dim liens As New List(Of ServiceDossiers.ParametresLienDossierEntrepriseContact)

        ' Pour chaque contact (ligne dans le tableau)
        For Each row As TableRow In Me.tbVotes.Rows
            Dim cleContact, clePouvoir As Integer
            Dim nomContact, zsCode As String
            Dim pouvoir As Boolean = False

            ' Lecture de la clé du contact
            If Not IsNumeric(row.Cells(0).Attributes("contact")) Then Continue For
            cleContact = CInt(row.Cells(0).Attributes("contact"))
            If cleContact <= 0 Then Continue For
            nomContact = row.Cells(0).Text

            ' Lecture du vote
            zsCode = ""
            For i As Integer = 1 To 4
                If CType(row.Cells(i).Controls(0), RadioButton).Checked Then
                    Select Case row.Cells(i).Attributes("vote").ToUpper
                        Case "OUI"
                            zsCode = Me.CodeZSTypeRelationVoteOui
                        Case "NON"
                            zsCode = Me.CodeZSTypeRelationVoteNon
                        Case "ABSTENTION"
                            zsCode = Me.CodeZSTypeRelationVoteAbstention
                        Case "ABSENT"
                            zsCode = Me.CodeZSTypeRelationVoteAbsent
                            pouvoir = True
                        Case Else
                            zsCode = ""
                    End Select
                    Exit For
                End If
            Next
            If zsCode = "" Then Continue For

            ' Lecture du contact ayant le pouvoir
            clePouvoir = 0
            If pouvoir Then
                If IsNumeric(CType(row.Cells(5).Controls(1), HiddenField).Value) Then
                    clePouvoir = CInt(CType(row.Cells(5).Controls(1), HiddenField).Value)
                End If
            End If

            ' Création du lien rapport / contact
            Using svc As New ServiceDossiers.DossiersClient
                Dim params As New ServiceDossiers.ParametresLienDossierEntrepriseContact
                params.Authentication = New ServiceDossiers.AuthenticationParameters
                params.Authentication.Token = Tools.GetInstance.getToken
                params.CodeDossier = Me.Rapport.CodeDossier
                params.CleContact = cleContact
                params.TypeRelation = Me.TypeRelationVote
                params.ZoneSup = New ServiceDossiers.ZoneSup
                params.ZoneSup.Code = zsCode
                params.ZoneSup.Numerique1 = clePouvoir

                Try
                    Dim results As ServiceDossiers.OperationResult
                    results = svc.AjouterLienActeur(params)
                    If results.OK Then
                        liens.Add(params)
                    Else
                        erreurs &= If(erreurs = "", "", ControlChars.NewLine) & "Erreur pendant l'enregistrement du vote de " & nomContact & " :" & results.ErrorMessage
                    End If
                Catch ex As Exception
                    erreurs &= If(erreurs = "", "", ControlChars.NewLine) & "Exception pendant l'enregistrement du vote de " & nomContact & " :" & ex.Message
                End Try
            End Using
        Next

        If erreurs = "" Then
            NotifySuccess("Votes enregistrés avec succès.")
            Me.Etape = CType(CInt(Me.Etape) + 1, EtapeVote)
            InitialiserEtape(True)
        Else
            ' Suppression des liens enregistrés
            Using svc As New ServiceDossiers.DossiersClient
                Try
                    For Each lien As ServiceDossiers.ParametresLienDossierEntrepriseContact In liens
                        svc.SupprimerLienActeur(lien)
                    Next
                Catch ex As Exception
                    erreurs &= If(erreurs = "", "", ControlChars.NewLine) & "Exception pendant la suppression des votes :" & ex.Message
                End Try
            End Using
            'Affichage des erreurs
            NotifyErreur(erreurs)
        End If
    End Sub
    Private Sub btnModifier_Click(sender As Object, e As EventArgs) Handles btnModifier.Click
        If Me.Etape <> EtapeVote.VisualisationVotes Then Return
        Me.Etape = EtapeVote.AffectationVotes
        InitialiserEtape(False)
    End Sub
    Private Sub btnNouveau_Click(sender As Object, e As EventArgs) Handles btnNouveau.Click
        ReinitialiserPage()
    End Sub


    Protected Sub ReinitialiserPage()
        Me.Etape = EtapeVote.ChoixAssemblee
        Me.Assemblee = Nothing
        Me.Rapport = Nothing
        ViewState("votes") = Nothing
        InitialiserEtape(True)
    End Sub
    Protected Sub InitialiserEtape(visuAutorisee As Boolean)
        Select Case Me.Etape
            Case EtapeVote.ChoixAssemblee
                Me.txtAssemblee.Visible = False
                Me.gvAssemblees.Visible = True
                Me.lblRapport.Visible = False
                Me.txtRapport.Visible = False
                Me.gvRapports.Visible = False
                Me.lblVotes.Visible = False
                Me.tbVotes.Visible = False
                Me.btnPrecedent.Visible = False
                Me.btnSuivant.Visible = True
                Me.btnAnnuler.Visible = False
                Me.btnEnregistrer.Visible = False
                Me.btnModifier.Visible = False
                Me.btnNouveau.Visible = False

                ' Chargement des assemblées
                Using svc As New DeliberationsClient
                    Dim params As New ParametresListe
                    params.Authentication = New AuthenticationParameters
                    params.Authentication.Token = Tools.GetInstance.getToken
                    params.Etat = Me.EtatAssembleeEnCours
                    Try
                        ' Recherche des dossiers et chargement du gridview associé
                        Dim result As ResultatListeDossiersDeliberationOfAssembleewn7zOZhO = svc.ListeAssemblees(params)
                        If result.OK Then
                            Me.gvAssemblees.DataSource = result.Dossiers
                            Me.gvAssemblees.DataBind()
                        Else
                            NotifyErreur(result.ErrorMessage)
                        End If
                    Catch ex As Exception
                        NotifyErreur(ex.Message)
                    End Try
                End Using


            Case EtapeVote.ChoixRapport
                Me.txtAssemblee.Visible = True
                Me.gvAssemblees.Visible = False
                Me.lblRapport.Visible = True
                Me.txtRapport.Visible = False
                Me.gvRapports.Visible = True
                Me.lblVotes.Visible = False
                Me.tbVotes.Visible = False
                Me.btnPrecedent.Visible = True
                Me.btnSuivant.Visible = True
                Me.btnAnnuler.Visible = False
                Me.btnEnregistrer.Visible = False
                Me.btnModifier.Visible = False
                Me.btnNouveau.Visible = False

                ' Initialisation de l'assemblée
                Me.txtAssemblee.Text = Me.Assemblee.CodeDossier & " - " & Me.Assemblee.Identificateur

                ' Chargement des rapports
                Using svc As New DeliberationsClient
                    Dim params As New ParametresListe
                    params.Authentication = New AuthenticationParameters
                    params.Authentication.Token = Tools.GetInstance.getToken
                    params.AssembleeLiee = Me.Assemblee.CodeDossier
                    Try
                        ' Recherche des dossiers et chargement du gridview associé
                        Dim result As ResultatListeDossiersDeliberationOfRapportwn7zOZhO = svc.ListeRapports(params)
                        If result.OK Then
                            Me.gvRapports.DataSource = result.Dossiers
                            Me.gvRapports.DataBind()
                        Else
                            NotifyErreur(result.ErrorMessage)
                        End If
                    Catch ex As Exception
                        NotifyErreur(ex.Message)
                    End Try
                End Using

            Case EtapeVote.AffectationVotes,
                 EtapeVote.VisualisationVotes
                Me.txtAssemblee.Visible = True
                Me.gvAssemblees.Visible = False
                Me.lblRapport.Visible = True
                Me.txtRapport.Visible = True
                Me.gvRapports.Visible = False
                Me.lblVotes.Visible = True
                Me.tbVotes.Visible = True
                Me.btnPrecedent.Visible = False
                Me.btnSuivant.Visible = False

                ' Initialisation de l'assemblée
                Me.txtAssemblee.Text = Me.Assemblee.CodeDossier & " - " & Me.Assemblee.Identificateur

                ' Initialisation du rapport
                Me.txtRapport.Text = Me.Rapport.CodeDossier & " - " & Me.Rapport.Identificateur

                ' Chargement des votes
                Dim voteEffectue As Boolean = False
                Using svc As New DeliberationsClient
                    Dim params As New ParametresLectureVotes
                    params.Authentication = New AuthenticationParameters
                    params.Authentication.Token = Tools.GetInstance.getToken
                    params.Commission = Me.Rapport.CommissionLiee
                    params.Rapport = Me.Rapport.CodeDossier

                    Try
                        Dim results As ResultatLectureVotes = svc.LireVotes(params)
                        If results Is Nothing OrElse Not results.OK Then
                            NotifyErreur("Impossible de charger les votes pour le rapport '" & Me.Rapport.CodeDossier & "' et la commission '" & Me.Rapport.CommissionLiee & "'" & If(results Is Nothing, ".", " :" & ControlChars.NewLine & results.ErrorMessage))
                            Return
                        Else
                            voteEffectue = results.VoteEffectue
                            ViewState("votes") = results.Votes
                        End If
                    Catch ex As Exception
                        NotifyErreur(ex.Message)
                        Return
                    End Try
                End Using

                If (voteEffectue AndAlso visuAutorisee) Then Me.Etape = EtapeVote.VisualisationVotes

                ' Chargement du tableau
                If Me.Etape = EtapeVote.AffectationVotes Then
                    InitialiserTableauSaisie()
                ElseIf Me.Etape = EtapeVote.VisualisationVotes Then
                    InitialiserTableauVisualisation()
                End If

                Me.btnAnnuler.Visible = (Me.Etape = EtapeVote.AffectationVotes)
                Me.btnEnregistrer.Visible = (Me.Etape = EtapeVote.AffectationVotes)
                Me.btnModifier.Visible = (Me.Etape = EtapeVote.VisualisationVotes)
                Me.btnNouveau.Visible = (Me.Etape = EtapeVote.VisualisationVotes)

        End Select

    End Sub
    Private Sub InitialiserTableauSaisie()
        Dim membresJS As String = ""

        If Me.Etape <> EtapeVote.AffectationVotes Then Return

        ' Génération de la liste des éléments de combo
        Dim li As New List(Of ListItem)
        membresJS = "['Aucun pouvoir', '']"
        li.Add(New ListItem("Aucun pouvoir", ""))
        For Each c As Vote In Me.Votes
            membresJS &= ",['" & c.Qualite & " " & c.Nom & If(c.Prenom = "", "", " " & c.Prenom) & "', '" & c.Cle.ToString & "']"
            li.Add(New ListItem(c.Qualite & " " & c.Nom & If(c.Prenom = "", "", " " & c.Prenom), c.Cle.ToString))
        Next

        ' Suppression des lignes
        Me.tbVotes.Rows.Clear()

        ' Ligne d'en-tête
        InitialiserEnTeteTableau(True)


        ' Autres lignes
        If Me.Votes.Count = 0 Then
            InitialiserLigneVide()
        Else
            For Each v As Vote In Me.Votes
                InitialiserLigneContact(v.Cle, v.Qualite & " " & v.Nom & If(v.Prenom = "", "", " " & v.Prenom), v.VoteMember = TypeVote.Oui, v.VoteMember = TypeVote.Non, v.VoteMember = TypeVote.Abstention, v.VoteMember = TypeVote.Absent, v.CleContactPouvoir, v.NomContactPouvoir, False, li)
            Next
        End If

        If membresJS <> "" Then ScriptManager.RegisterArrayDeclaration(Me, "membres", membresJS)

        ScriptManager.RegisterStartupScript(Me, Me.GetType, "initVotes", "gererContactsPouvoir();", True)
    End Sub
    Private Sub InitialiserTableauVisualisation()
        Dim membresJS As String = ""

        If Me.Etape <> EtapeVote.VisualisationVotes Then Return

        ' Suppression des lignes
        Me.tbVotes.Rows.Clear()

        ' Ligne d'en-tête
        InitialiserEnTeteTableau(False)

        ' Autres lignes
        If Me.Votes.Count = 0 Then
            InitialiserLigneVide()
        Else
            Dim nbOui As Integer = 0
            Dim nbNon As Integer = 0
            Dim nbAbstention As Integer = 0
            Dim nbAbsent As Integer = 0

            For Each v As Vote In Me.Votes
                InitialiserLigneContact(v.Cle, v.Qualite & " " & v.Nom & If(v.Prenom = "", "", " " & v.Prenom), v.VoteMember = TypeVote.Oui, v.VoteMember = TypeVote.Non, v.VoteMember = TypeVote.Abstention, v.VoteMember = TypeVote.Absent, v.CleContactPouvoir, v.NomContactPouvoir, True, Nothing)
                If v.VoteMember = TypeVote.Oui Then nbOui += 1
                If v.VoteMember = TypeVote.Non Then nbNon += 1
                If v.VoteMember = TypeVote.Abstention Then nbAbstention += 1
                If v.VoteMember = TypeVote.Absent Then
                    nbAbsent += 1
                    If v.CleContactPouvoir > 0 Then
                        For Each pouvoir As Vote In Me.Votes
                            If pouvoir.Cle = v.CleContactPouvoir Then
                                If pouvoir.VoteMember = TypeVote.Oui Then nbOui += 1
                                If pouvoir.VoteMember = TypeVote.Non Then nbNon += 1
                                If pouvoir.VoteMember = TypeVote.Abstention Then nbAbstention += 1
                                Exit For
                            End If
                        Next
                    End If
                End If
            Next

            ' Ligne de pied
            InitialiserPiedPageTableau(nbOui, nbNon, nbAbstention, nbAbsent)
        End If

        If membresJS <> "" Then ScriptManager.RegisterArrayDeclaration(Me, "membres", membresJS)
    End Sub
    Private Sub InitialiserEnTeteTableau(boutonsSelection As Boolean)
        Dim row As TableRow
        Dim cell As TableCell
        Dim lbl As Label = Nothing
        Dim imgBtn As ImageButton = Nothing
        Dim spacer As Literal = Nothing

        row = New TableRow()

        ' En-tête de ligne (nom du membre)
        cell = New TableHeaderCell()
        cell.Attributes.Add("scope", "col")
        cell.Width = Unit.Percentage(16)
        cell.Text = ""
        row.Cells.Add(cell)

        ' Colonne 'Oui'
        cell = New TableHeaderCell()
        cell.Attributes.Add("scope", "col")
        cell.Width = Unit.Percentage(17)
        If (boutonsSelection) Then
            InitialiserEnTeteSelectionnerTout(spacer, lbl, imgBtn)
            lbl.Text = "Oui"
            imgBtn.OnClientClick = "selectionnerTout('oui'); return false;"
            cell.Controls.Add(lbl)
            cell.Controls.Add(spacer)
            cell.Controls.Add(imgBtn)
        Else
            cell.Text = "Oui"
        End If
        row.Cells.Add(cell)

        ' Colonne 'Non'
        cell = New TableHeaderCell()
        cell.Attributes.Add("scope", "col")
        cell.Width = Unit.Percentage(17)
        If (boutonsSelection) Then
            InitialiserEnTeteSelectionnerTout(spacer, lbl, imgBtn)
            lbl.Text = "Non"
            imgBtn.OnClientClick = "selectionnerTout('non'); return false;"
            cell.Controls.Add(lbl)
            cell.Controls.Add(spacer)
            cell.Controls.Add(imgBtn)
        Else
            cell.Text = "Non"
        End If
        row.Cells.Add(cell)

        ' Colonne 'Abstention'
        cell = New TableHeaderCell()
        cell.Attributes.Add("scope", "col")
        cell.Width = Unit.Percentage(17)
        If (boutonsSelection) Then
            InitialiserEnTeteSelectionnerTout(spacer, lbl, imgBtn)
            lbl.Text = "Abstention"
            imgBtn.OnClientClick = "selectionnerTout('abstention'); return false;"
            cell.Controls.Add(lbl)
            cell.Controls.Add(spacer)
            cell.Controls.Add(imgBtn)
        Else
            cell.Text = "Abstention"
        End If
        row.Cells.Add(cell)

        ' Colonne 'Absent'
        cell = New TableHeaderCell()
        cell.Attributes.Add("scope", "col")
        cell.Width = Unit.Percentage(17)
        If (boutonsSelection) Then
            InitialiserEnTeteSelectionnerTout(spacer, lbl, imgBtn)
            lbl.Text = "Absent"
            imgBtn.OnClientClick = "selectionnerTout('absent'); return false;"
            cell.Controls.Add(lbl)
            cell.Controls.Add(spacer)
            cell.Controls.Add(imgBtn)
        Else
            cell.Text = "Absent"
        End If
        row.Cells.Add(cell)

        ' Colonne 'Pouvoir'
        cell = New TableHeaderCell()
        cell.Attributes.Add("scope", "col")
        cell.Width = Unit.Percentage(16)
        cell.Text = "Pouvoir"
        row.Cells.Add(cell)

        Me.tbVotes.Rows.Add(row)
    End Sub
    Private Sub InitialiserLigneVide()
        Dim row As New TableRow()
        Dim cell As New TableCell()

        cell.Text = If(Me.Rapport.CommissionLiee = "", "Le rapport '" & Me.Rapport.CodeDossier & "' n'est associé à aucune commission.",
                                                       "La commission '" & Me.Rapport.CommissionLiee & "' ne compte aucun membre.")
        cell.ColumnSpan = 6
        cell.HorizontalAlign = HorizontalAlign.Center
        row.Cells.Add(cell)

        Me.tbVotes.Rows.Add(row)
    End Sub
    Private Sub InitialiserLigneContact(cleContact As Integer, nomContact As String, voteOui As Boolean, voteNon As Boolean, voteAbstention As Boolean, voteAbsent As Boolean, clePouvoir As Integer, nomPouvoir As String, lectureSeule As Boolean, cbItems As List(Of ListItem))
        Dim row As New TableRow()

        ' En-tête de ligne (nom du membre)
        InitialiserCelluleText(row, nomContact, True, cleContact)

        ' Colonne 'Oui'
        InitialiserCelluleRadio(row, cleContact, "oui", voteOui, Not lectureSeule)

        ' Colonne 'Non'
        InitialiserCelluleRadio(row, cleContact, "non", voteNon, Not lectureSeule)

        ' Colonne 'Abstention'
        InitialiserCelluleRadio(row, cleContact, "abstention", voteAbstention, Not lectureSeule)

        ' Colonne 'Absent'
        InitialiserCelluleRadio(row, cleContact, "absent", voteAbsent, Not lectureSeule)

        ' Colonne 'Pouvoir'
        If lectureSeule Then
            Dim nom As String = ""
            If nomPouvoir <> "" Then
                nom = nomPouvoir
            ElseIf voteAbsent Then
                clePouvoir = 0
                nom = "Aucun pouvoir"
            End If
            InitialiserCelluleText(row, nom, False, clePouvoir)
        Else
            InitialiserCelluleCombo(row, cbItems, voteAbsent, clePouvoir)
        End If

        Me.tbVotes.Rows.Add(row)
    End Sub
    Private Sub InitialiserCelluleText(row As TableRow, text As String, enTete As Boolean, cleContact As Integer)
        Dim cell As TableCell

        If enTete Then
            cell = New TableHeaderCell()
            cell.Attributes.Add("scope", "row")
            cell.Attributes.Add("contact", cleContact.ToString)
        Else
            cell = New TableCell()
        End If

        cell.Text = text

        row.Cells.Add(cell)
    End Sub
    Private Sub InitialiserCelluleRadio(row As TableRow, cleContact As Integer, vote As String, checked As Boolean, enabled As Boolean)
        Dim cell As New TableCell()
        Dim rb As New RadioButton()

        rb.Checked = checked
        rb.Enabled = enabled
        rb.GroupName = cleContact.ToString
        cell.Controls.Add(rb)

        cell.Attributes.Add("vote", vote)
        cell.HorizontalAlign = HorizontalAlign.Center

        row.Cells.Add(cell)
    End Sub
    Private Sub InitialiserCelluleCombo(row As TableRow, items As List(Of ListItem), enabled As Boolean, clePouvoir As Integer)
        Dim cell As New TableCell()
        Dim cb As New HtmlSelect
        Dim hidden As New HiddenField()

        If Not enabled Then cb.Attributes.Add("disabled", "disabled")
        cb.Attributes.Add("onchange", "affecterValeur(this);")
        cb.Items.AddRange(items.ToArray)
        cb.SelectedIndex = 0
        cb.Style.Add("width", "100%")
        cell.Controls.Add(cb)

        cell.Controls.Add(hidden)

        cell.Attributes.Add("pouvoir", "1")
        cell.HorizontalAlign = HorizontalAlign.Center

        row.Cells.Add(cell)

        If (clePouvoir <> 0) Then
            For i As Integer = 0 To cb.Items.Count - 1
                If cb.Items(i).Value = clePouvoir.ToString Then
                    cb.SelectedIndex = i
                    hidden.Value = clePouvoir.ToString
                    Exit For
                End If
            Next
        End If
    End Sub
    Private Sub InitialiserEnTeteSelectionnerTout(ByRef spacer As Literal, ByRef lbl As Label, ByRef imgBtn As ImageButton)
        spacer = New Literal
        spacer.Text = " "

        lbl = New Label()

        imgBtn = New ImageButton
        imgBtn.AlternateText = "Tout"
        imgBtn.Height = Unit.Pixel(20)
        imgBtn.ImageUrl = "~/Images/check_all_white_20.png"
        imgBtn.Style.Add("vertical-align", "text-top")
        imgBtn.ToolTip = "Sélectionner tout"
        imgBtn.Width = Unit.Pixel(20)
    End Sub
    Private Sub InitialiserPiedPageTableau(nbOui As Integer, nbNon As Integer, nbAbstention As Integer, nbAbsent As Integer)
        Dim row As TableRow
        Dim cell As TableCell

        row = New TableFooterRow()

        ' En-tête de ligne (total)
        cell = New TableHeaderCell()
        cell.Attributes.Add("scope", "row")
        cell.Text = "TOTAL"
        row.Cells.Add(cell)

        ' Colonne 'Oui'
        cell = New TableHeaderCell()
        cell.Text = nbOui.ToString
        row.Cells.Add(cell)

        ' Colonne 'Non'
        cell = New TableHeaderCell()
        cell.Text = nbNon.ToString
        row.Cells.Add(cell)

        ' Colonne 'Abstention'
        cell = New TableHeaderCell()
        cell.Text = nbAbstention.ToString
        row.Cells.Add(cell)

        ' Colonne 'Absent'
        cell = New TableHeaderCell()
        cell.Text = nbAbsent.ToString
        row.Cells.Add(cell)

        ' Colonne 'Pouvoir'
        cell = New TableHeaderCell()
        row.Cells.Add(cell)

        Me.tbVotes.Rows.Add(row)
    End Sub

End Class